<template>
  <div class="home-container">
    <!-- 导航栏 -->
     <!-- 导航栏 -->
     <van-nav-bar class="page-nav-bar" fixed>
      <van-button slot="title" class="search-btn"
      type="info"
      size="small"
      round
      icon="search"
      to="/search"
      >搜索</van-button>
    </van-nav-bar>
    <!-- 导航栏结束 -->
    <!-- 频道列表 -->
    <van-tabs class="channel-tabs" v-model="active" animated swipeable>
      <van-tab :title="channel.name"
       v-for="channel in channels" :key="channel.id">
       <!-- 文章列表 -->
      <article-list :channel="channel"></article-list>
      <!-- 文章列表 -->
      </van-tab>

      <div slot="nav-right" class="placeholder"></div>
      <div slot="nav-right" class="hamburger-btn" @click="ischannelEditShow = true">
        <i class="toutiao toutiao-gengduo"></i>
      </div>
    </van-tabs>
    <!-- 频道编辑 -->
    <van-popup
    v-model="ischannelEditShow"
    closeable
    close-icon-position="top-left"
    position="bottom"
    :style="{ height: '95%' }"
  >
  <ChannelEdit :my-channels="channels"
    :active="active"
    @update-active="onUpdateActive"
  ></ChannelEdit>
</van-popup>
    <!-- 频道编辑 -->

  </div>
</template>

<script>
import { getUserChannels } from '@/api/user'
import ArticleList from './components/article-list.vue'
import ChannelEdit from './components/channel-edit.vue'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage'
export default {
  name: 'HomeIndex',
  data () {
    return {
      // 通过 v-model 绑定当前激活标签对应的索引值，默认情况下启用第一个标签。
      active: 0,
      channels: [], // 频道列表数组
      ischannelEditShow: false // 控制编辑弹出层的展示
    }
  },
  created () {
    this.loadChannels()
  },
  methods: {
    async loadChannels () {
      try {
        let channels = []
        if (this.user) {
          // 已登录，请求获取用户频道列表
          const { data } = await getUserChannels()
          channels = data.data.channels
        } else {
          // 未登录，判断是否有本地频道数据
          const localChannels = getItem('TOUTIAO_CHANNELS')
          // 有，直接用
          if (localChannels) {
            channels = localChannels
          } else {
            // 没有，请求默认频道列表
            const { data } = await getUserChannels()
            channels = data.data.channels
          }
        }
        this.channels = channels
      } catch (error) {
        this.$toast('获取频道数据失败')
      }
    },
    onUpdateActive (index, ischannelEditShow = true) {
      // 更新激活的频道项
      this.active = index
      // 关闭频道弹出层
      this.ischannelEditShow = ischannelEditShow
    }
  },
  components: {
    ArticleList,
    ChannelEdit
  },
  computed: {
    ...mapState(['user'])
  }
}
</script>

<style lang="less" scoped>
.home-container {
  padding-top: 87px;
  padding-bottom: 50px;
  .van-nav-bar {
    background-color: #3926fa;
    .van-nav-bar__title {
      max-width: unset;
    }
  }
  .search-btn {
    width: 200px;
    height: 32px;
    background-color: #5babfb;
    border: none;
    font-size: 17px;
    .van-icon {
      font-size: 20px;
      color: white;
    }
  }
  /deep/ .channel-tabs {
     .van-tabs__wrap {
       height: 41px;
        left: 0;
        z-index: 1;
        position: fixed;
        top: 46px;
        right: 0;
      }
    .van-tab {
      min-width: 100px;
      font-size: 15px;
      border-right: 1px solid #edeff3;
      color: #777;
      .van-tab--active {
        color: #333;
      }
      .van-tabs__nav {
        padding-bottom: 0;
      }
      .van-tabs__line  {
        width: 15.5px !important;
        height: 3px;
        background-color: #3926fa;
        bottom: 4px ;
      }

    }
  }
  .placeholder {
    width: 33px;
    height: 41px;
    flex-shrink: 0;
  }
  .hamburger-btn {
    position: fixed;
    right: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 33px;
    height: 41px;
    background-color: #fff;
    opacity: 0.902;
    i.toutiao {
      font-size: 16.5px;
    }
    &::before {
      content: '';
      position: absolute;
      left: 0;
      width: 1px;
      height: 100%;
      background: url('@/assets/gradient-gray-line.png');
      background-size: contain;
    }
  }
}

</style>
